package com.shop.common.partner.sms;

import com.alibaba.fastjson.JSONObject;
import com.shop.util.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class SMSZhuanXinImpl implements ISMS {

    @Override
    public boolean sendCode(String mobile, String code) {
        String content = "【唐古拉优选】验证码：" + code + "，如非本人操作请忽略。";
        log.info("mobile: " + mobile + ", content: " + content);
        return sendMessage(mobile, content);
    }

    @Override
    public boolean sendMessage(String mobile, String content) {
        String host = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
        String appKey = "plhtWejNtwqYQmVqyo51WOaVECrbUMyk";
        String appSecret = "86055f84a75f9a335370c3a643fe085f624";
        Map<String, String> headers = new HashMap<String, String>();

        //拼接url
        StringBuilder url = new StringBuilder();
        url.append(host).append("?appKey=").append(appKey).append("&appSecret=").append(appSecret).append("&phones=").append(mobile).append("&content=").append(content);

        try {
            Map<String, Object> response = HttpUtil.get(url.toString(), headers);
            SMSZhuanxinReturesms smsZhuanxinyun = null;
            if (response.get("body") != null) {
                smsZhuanxinyun = JSONObject.parseObject(response.get("body").toString(), SMSZhuanxinReturesms.class);
                if (smsZhuanxinyun.getErrorCode().equals("000000")) {
                    return true;
                }
                log.error("短信验证码发送失败！失败原因：" + smsZhuanxinyun.getErrorMsg());
            }
        } catch (Exception e) {
            log.error("短信发送异常", e);
            return false;
        }

        return false;
    }

}
